package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.CommodityType;
import com.erp.bean.Supplier;
import com.erp.service.CommodityService;
import com.erp.service.CommodityTypeService;
import com.erp.service.SupplierService;
import com.erp.service.impl.CommodityServiceImpl;
import com.erp.service.impl.CommodityTypeServiceImpl;
import com.erp.service.impl.SupplierServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ld
 * 商品关键字搜索控制器
 */

@WebServlet("/commoditysearch.action")
public class CommoditySearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建业务逻辑对象
        CommodityService commodityService=new CommodityServiceImpl();
        //创建供应商业务逻辑对象
        SupplierService supplierService = new SupplierServiceImpl();
        //创建商品类业务逻辑对象
        CommodityTypeService commodityTypeService = new CommodityTypeServiceImpl();
        //获取查询类型
        String str=req.getParameter("keywords");
        System.out.println(str);
        //根据关键字查询商品信息

        List <Commodity> list=commodityService.find("gname",str);
        System.out.println(list);
        //查询供应商信息
        List<Supplier> list1 = supplierService.findAll();
        System.out.println(list1);
        //查询商品类型信息
        List<CommodityType> list2 = commodityTypeService.findAll();
        System.out.println(list2);
        //将商品信息放入作用域中
        req.setAttribute("list",list);
        //将供应商放入作用域中
        req.setAttribute("list1",list1);
        //将商品类型放入作用域中
        req.setAttribute("list2",list2);
        //转发到商品列表页面
        req.getRequestDispatcher("commoditylist.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
